package com.zeek.car90s.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.util.Log;

import com.zeek.car90s.Constants;

//www.javaapk.com
public class RandomArray {
	TxtReader txtr = new TxtReader();

	public List listdate(InputStream inputStream,String answer) {
		List<String> list=new ArrayList<String>();
		String  randss[]=txtr.getString(inputStream);

		answer=answer.replace(" ","");

		int z=(int)(Math.random()*(100));
		Random r = new Random(z);
		int len=Constants.ANSWER_COUNT-answer.length();
		Log.d("LEN",String.valueOf(len));
//		Set<String> set = new HashSet<String>();
		ArrayList<String> set=new ArrayList<>();

		String strarray[] = new String[len];
		/***把answer添加到数组里********/
		char[] chars=answer.toCharArray();
		for(int i=0;i<chars.length;i++){
			set.add(String.valueOf(chars[i]));
		}
		/*************/

		int index = 0;
		for (int i = 0; i < len; i++) {
			// 刚开始从数组中随机抽取一个
			// 而后将抽取的元素后面的元素向前推进到随机的位置[index位置]
			// 随着循环的继续,逐渐抛弃后面的元素
			index = r.nextInt(randss.length - i);
			strarray[i] = randss[index];
			// 元素向前推进到随机[index]的位置
			for (int j = index; j < randss.length - i - 1; j++) {
				randss[j] = randss[j + 1];
			}
		}

		//set.add(String.valueOf(chars[]))
		// 增强for循环 遍历strarray数组的值
		for (String string : strarray) {
			//System.out.print(string + " ");
			set.add(string);
		}
//		System.out.println("郑伟"+strarray.length);
//		System.out.println(set+","+set.size());
		list.addAll(set);
		while(list.size()!= Constants.ANSWER_COUNT){
			int v=(int)(Math.random()*(200));
			list.add(randss[v]);
		}

		Collections.shuffle(list);//洗牌，对里面的元素随机排列一次
//		System.out.println(list+","+list.size());
//		Log.d("LIST", list.get(0).toString());
		//Log.d("SET", msg);
		return list;
	}

	//英语单词和汉语不同，不能弄那么多字母
	public List listdateForEnglish(InputStream inputStream,String answer) {

		List<String> list=new ArrayList<String>();
		String  randss[]=txtr.getString(inputStream);
		answer=answer.replace(" ","");

		int z=(int)(Math.random()*(100));
		Random r = new Random(z);

		int answerShowCount=getAnswerCount(answer.length());
		int len=answerShowCount-answer.length();
		Log.d("LEN",String.valueOf(len));
//		Set<String> set = new HashSet<String>();
		ArrayList<String> set=new ArrayList<>();

		String strarray[] = new String[len];
		/***把answer添加到数组里********/
		char[] chars=answer.toCharArray();
		for(int i=0;i<chars.length;i++){
			set.add(String.valueOf(chars[i]));
		}
		/*************/

		int index = 0;
		for (int i = 0; i < len; i++) {
			// 刚开始从数组中随机抽取一个
			// 而后将抽取的元素后面的元素向前推进到随机的位置[index位置]
			// 随着循环的继续,逐渐抛弃后面的元素
			index = r.nextInt(randss.length - i);
			strarray[i] = randss[index];
			// 元素向前推进到随机[index]的位置
			for (int j = index; j < randss.length - i - 1; j++) {
				randss[j] = randss[j + 1];
			}
		}

		//set.add(String.valueOf(chars[]))
		// 增强for循环 遍历strarray数组的值
		for (String string : strarray) {
			//System.out.print(string + " ");
			set.add(string);
		}


		list.addAll(set);
		//补充混淆元素
		while(list.size()!= answerShowCount){
			int v=(int)(Math.random()*(200));
			list.add(randss[v]);
		}

		Collections.shuffle(list);//洗牌，对里面的元素随机排列一次
//		System.out.println(list+","+list.size());
//		Log.d("LIST", list.get(0).toString());
		//Log.d("SET", msg);
		return list;
	}

	//根据正确答案的长度决定最终显示多少格子
	private int getAnswerCount(int answerCount){
		if(answerCount<7){
			return 10;
		}else if(answerCount>=7&&answerCount<11){
			return 12;
		}else if(answerCount>=11&&answerCount<=14){
			return 14;
		}else if(answerCount>14&&answerCount<15){
            return 15;
        }else if(answerCount>=15&&answerCount<17){
			return 18;
		}else {
			return 21;
		}
	}
}